CtgMunicipio = function(){
	return {
		confirmProcess: function(option, ctgMunicipioId, ctgMunicipioActivo){
			Asg.Message.confirm(
					option == "delete" ? Asg.constants.CONFIRM_DELETE_MESSAGE :
						(ctgMunicipioActivo == "1" ? Asg.constants.CONFIRM_INACTIVATE_MESSAGE : Asg.constants.CONFIRM_ACTIVATE_MESSAGE), function(btn){
				if(btn != "no") CtgMunicipio.processRegister(option, ctgMunicipioId, ctgMunicipioActivo);
			});
		},
		processRegister: function(option, ctgMunicipioId, ctgMunicipioActivo){
			Asg.Message.progress("Guardarndo Informaci\u00F3n...");
			Ext.Ajax.request({
				timeout: Asg.constants.TIMEOUT_SECONDS,
				url: option == "delete" ? Asg.constants.CONTEXT_PATH + "/admin/municipios/delete" : Asg.constants.CONTEXT_PATH + "/admin/municipios/toggle",
				params: {ctgMunicipioId: ctgMunicipioId, ctgMunicipioActivo: ctgMunicipioActivo},
				callback: function(options, success, response){
					Ext.Msg.hide();
					Asg.Form.switchButton(CtgMunicipio.getButtonsConfig(), "cancel");
					var jsonObject = Asg.Utils.ajaxRequestGetJson(response);
					if(!success || !jsonObject){
						Asg.Message.alertInvalid(Asg.constants.DEFAULT_ERROR_MESSAGE);
						return
					}
					Asg.Message.alert(jsonObject.message || Asg.constants.DEFAULT_ERROR_MESSAGE);
					if(jsonObject.success){
						if(jsonObject.data) 
							Asg.Grid.loadData("ctgMunicipioGrid", jsonObject.data);
						Asg.Form.setEnableAndClear("ctgMunicipioForm", false);
						return;
					}
				}
			});
		},
		save: function(){
			Asg.Message.progress("Guardando Informaci\u00F3n...");
			Ext.getCmp("ctgMunicipioForm").getForm().submit({
    			url: Asg.constants.CONTEXT_PATH + "/admin/municipios/save",
    			timeout: Asg.constants.TIMEOUT_SECONDS,
    			success: function(form, action){
    				Asg.Message.confirmProcess(action.result.message, function(){
    					if(action.result.success){
    						Asg.Grid.clearSelection("ctgMunicipioGrid");
    						if(action.result.data) 
    							Asg.Grid.loadData("ctgMunicipioGrid", action.result.data);
    						if(!Ext.isEmpty(action.result.dataIndex)){
    							var dataIndex = Ext.getCmp("ctgMunicipioGrid").getStore().find("ctgMunicipio.ctgMunicipioId", action.result.dataIndex);
    							Ext.getCmp("ctgMunicipioGrid").getSelectionModel().select(dataIndex);
    						}
    					}
    				});
    			},
    			failure: Asg.Form.failureProcedure
   			});
		},
		getButtonsConfig: function(){
			return {
				add: "ctgMunicipioAddButton",
				edit: "ctgMunicipioEditButton",
				save: "ctgMunicipioSaveButton",
				cancel: "ctgMunicipioCancelButton"
			};
		},
		initConfig: function(config){
			return {
				items: [
			        {
			        	xtype: "grid",
			        	id: "ctgMunicipioGrid",
			        	frame: true,
			        	margins: "5 5 5 5",
			        	height: 250,
			        	store: new Ext.data.SimpleStore({
			        		data: config.data || [],
			        		fields: [
		        		        "ctgMunicipio.ctgMunicipioId", 
		        		        "ctgMunicipio.ctgMunicipioNombre", 
		        		        "ctgMunicipio.ctgMunicipioActivo", 
		        		        "ctgMunicipio.ctgDepartamento.ctgCatalogoId",
		        		        "ctgMunicipio.ctgDepartamento.ctgCatalogoNombre"
		    		        ],
		    		        groupers: [{property: "ctgMunicipio.ctgDepartamento.ctgCatalogoNombre"}]
			        	}),
			        	features:[ 
			        		{
				        	   ftype: "grouping",
				        	   enableNoGroups: false,
				        	   groupHeaderTpl: "{name}"
				           }
		        		],
			        	selModel: {
		        			listeners: {
		        				selectionchange: function(selectionModel, records){
		        					if(records && records.length > 0){
		        						Asg.Form.setValues("ctgMunicipioForm", records[0].data);
		        						Asg.Form.switchButton(CtgMunicipio.getButtonsConfig(), "rowclick");
		    	        	    		Asg.Form.switchForm("ctgMunicipioForm", "rowclick");
		        					}
		        				}
		        			}
		        		},
			        	columns: [
		        	        {header: "Nombre", dataIndex: "ctgMunicipio.ctgMunicipioNombre", flex: 1, minWidth: 200},  
		        	        {header: "Estado", dataIndex: "ctgMunicipio.ctgMunicipioActivo", renderer: "webToActive", width: 100},
		        	        {
		        	        	xtype:"actioncolumn",
		        	        	align: "center",
		        	        	width: 60,
	        	            	items: [
        	            	        {
		        	            		icon: Asg.constants.CONTEXT_PATH + "/resources/images/enableDisable.png",
		        	            		iconCls: "icon",
		        	            		tooltip: "Activar / Inactivar", 
		        	            		handler: function(grid, rowIndex, colIndex) {
		        	                        var record = grid.getStore().getAt(rowIndex);
		        	                        if(record){
		        	                        	CtgMunicipio.confirmProcess("toggle", record.get("ctgMunicipio.ctgMunicipioId"), record.get("ctgMunicipio.ctgMunicipioActivo"));
		        	                        }
		        	            		}
	    	            			},{
		        	            		icon: Asg.constants.CONTEXT_PATH + "/resources/images/delete.png",
		        	            		iconCls: "icon",
		        	            		tooltip: "Eliminar",
		        	            		handler: function(grid, rowIndex, colIndex) {
		        	                        var record = grid.getStore().getAt(rowIndex);
		        	                        if(record){
		        	                        	CtgMunicipio.confirmProcess("delete", record.get("ctgMunicipio.ctgMunicipioId"), record.get("ctgMunicipio.ctgMunicipioActivo"));
		        	                        }
		        	            		}
	    	            			}
    	            	        ]
						    }
			            ],
			            bbar: [
		        	       "->",
		        	       {
		        	    	   id: "ctgMunicipioAddButton",
		        	    	   text: "Agregar", 
		        	    	   iconCls: Asg.constants.ADD_ICON,
		        	    	   handler: function(){
		        	    		   Asg.Form.setRequired("ctgMunicipioClave", true);
		        	    		   Asg.Form.setRequired("ctgMunicipioClave1", true);
		        	    		   Asg.Form.setAllowEnable("ctgMunicipioUsuario", true);
		        	    		   Asg.Form.switchButton(CtgMunicipio.getButtonsConfig(), "add");
		        	    		   Asg.Form.switchForm("ctgMunicipioForm", "add");
		        	    		   Asg.Grid.clearSelection("ctgMunicipioGrid");
		        	    	   }
	        	    	   },{
	        	    		   id: "ctgMunicipioEditButton",
	        	    		   text: "Editar", 
	        	    		   iconCls: Asg.constants.EDIT_ICON,
	        	    		   hidden: true,
	        	    		   handler: function(){
	        	    			   Asg.Form.setRequired("ctgMunicipioClave", false);
		        	    		   Asg.Form.setRequired("ctgMunicipioClave1", false);
		        	    		   Asg.Form.setAllowEnable("ctgMunicipioUsuario", false);
	        	    			   Asg.Form.switchButton(CtgMunicipio.getButtonsConfig(), "edit");
	        	    			   Asg.Form.switchForm("ctgMunicipioForm", "edit");
	        	    		   }
        	    		   },{
        	    			   id: "ctgMunicipioSaveButton",
        	    			   text: "Guardar", 
        	    			   iconCls: Asg.constants.SAVE_ICON,
        	    			   hidden: true,
        	    			   handler: CtgMunicipio.save
    	    			   },{
    	    				   id: "ctgMunicipioCancelButton",
    	    				   text: "Cancelar", 
    	    				   iconCls: Asg.constants.CANCEL_ICON,
    	    				   hidden: true,
    	    				   handler: function(){
	        	    			   Asg.Form.switchButton(CtgMunicipio.getButtonsConfig(), "cancel");
	        	    			   Asg.Form.switchForm("ctgMunicipioForm", "cancel");
	        	    			   Asg.Grid.clearSelection("ctgMunicipioGrid");
	        	    		   }
	    				   }
	        	        ]
			        },{
			        	title: "PLANES DE SEGUROS",
			        	margins: "0 5 5 5",
        	        	bodyPadding: "5 5 5 5",
			        	flex: 1,
        	        	autoScroll: true,
        	        	items: {
        	        		xtype: "form",
        	        		id: "ctgMunicipioForm",
        	        		border: false,
        	        		height: 450,
        	        		layout: {
	        	        		type: "table",
								columns: 1,
								tableAttrs: {
						            style: {width: "520px"},
						            align: "center"
								}
	        	        	},
	        	        	defaults: Asg.Form.defaults({width: 515}),
	        	        	items: [
	        	        		{xtype: "label", text: "Departamento:", cls: "x-form-item"},
	        	        		{
    	        	        		xtype: "combo",
    	        	        		name: "ctgMunicipio.ctgDepartamento.ctgCatalogoId",
    	        	        		allowBlank: false,
    	        	        		disabled: true,
    	        	        		store: new Ext.data.SimpleStore({
    	        	        			data: config.ctgDepartamentos || [],
    	        	        			fields: ["id", "value"]
    	        	        		}),
    	        	        		displayField: "value",
    	        	        		valueField: "id"
	        	        		},
	        	        		{xtype: "label", text: "Nombre:", cls: "x-form-item", colspan: 2},
    	        	        	{
    	        	        		xtype: "textfield",
    	        	        		name: "ctgMunicipio.ctgMunicipioNombre",
    	        	        		disabled: true,
    	        	        		width: 515,
    	        	        		colspan: 2
	        	        		},{
	        	        			xtype: "hidden",
	        	        			name: "ctgMunicipio.ctgMunicipioId"
	        	        		},{
	        	        			xtype: "hidden",
	        	        			name: "ctgMunicipio.ctgMunicipioActivo",
	        	        			defaultValue: "1"
	        	        		}
    	        	        ]
        	        	}
			        }
		        ]
			};
		}
	};
}();